package com.liu.controller;

import com.liu.dto.CollectDTO;
import com.liu.dto.ReadRecordDTO;
import com.liu.pojo.Chapter;
import com.liu.pojo.Novel;
import com.liu.util.HttpServletFactory;
import com.liu.util.LoggerFactory;
import com.liu.util.Page;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/7/21 12:02
 * @Description
 */
@WebServlet("/novel/*")
public class NovelServlet extends GenericServlet implements HttpServletFactory, LoggerFactory {

    public void _doListNovel(HttpServletRequest request, HttpServletResponse response) {

        try {

            Integer user_id = getSessionUser().getUser_id();

            List<Novel> list = novelDao.queryListByUserid(user_id);

            if (list.size() > 0) {
                request.setAttribute("list", list);
            } else {
                request.setAttribute("msg", "抱歉，暂未发现你上传的小说！");
            }

            new GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.PUBLISH_NOVEL.get())
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("查询小说异常！", e);
        }
    }

    public void _doPublish(HttpServletRequest request, HttpServletResponse response) {

        try {

            Integer novel_id = Integer.parseInt(request.getParameter("novel_id"));
            String author_name = request.getParameter("author_name");

            request.setAttribute("novel_id", novel_id);
            request.setAttribute("author_name", author_name);

            new GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.PUBLISH.get())
                    .build()
                    .getRequestMapping();
        } catch (Exception e) {
            logger.error("跳转异常！", e);
        }
    }

    public void _doPublishNewChapter(HttpServletRequest request, HttpServletResponse response) {

        try {

            Integer novel_id = Integer.parseInt(request.getParameter("novel_id"));
            String title = request.getParameter("chapter");
            String content = request.getParameter("content");

            Chapter o = new Chapter();
            o.setNovel_id(novel_id);
            o.setTitle(title);
            o.setContent(content);
            o.setRead_count(0);
            o.setPush_time(new Timestamp(System.currentTimeMillis()));
            o.setSort(1);

            int result = chapterDao.save(o);

            if (result > 0) {
                request.setAttribute("msg", "发布成功");
            } else {
                request.setAttribute("msg", "发布失败，系统错误！");
            }

            new GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.PUBLISH.get())
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("发布新章节异常！", e);
        }
    }

    public void _doSearch(HttpServletRequest request, HttpServletResponse response) {

        try {

            String keyboard = request.getParameter("keyboard");
            List<Novel> list = novelDao.queryListByKeyword(keyboard);

            if (list.size() > 0) {
                request.setAttribute("list", list);
            } else {
                request.setAttribute("msg", "抱歉，暂未发现你上传的小说！");
            }

            new GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.PUBLISH_NOVEL.get())
                    .build()
                    .getRequestMapping();


        } catch (Exception e) {
            logger.error("搜素异常！", e);
        }
    }

    public void _doFavorite(HttpServletRequest request, HttpServletResponse response) {

        try {

            Integer user_id = getSessionUser().getUser_id();
            List<CollectDTO> list = favoriteDao.queryFavoriteByUser_id(user_id);

            if (list.size() > 0) {
                request.setAttribute("clist", list);
            } else {
                request.setAttribute("nmsg", "暂无信息");
            }

            new GenericServletBuilder(request, response, this.getClass())
                    .setUrl(Page.COLLECT.get())
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("收藏查询异常！", e);
        }
    }

    //会员收藏删除
    public void _doDelete(HttpServletRequest request, HttpServletResponse response) {

        try {

            String author_name = request.getParameter("author_name");

            Integer result = favoriteDao.delete(author_name);

            if (result > 0) {
                //成功后跳转 _doCollectData方法重新查询返回界面
            } else {
                //失败后直接反面收藏界面，并打印错误信息
                request.setAttribute("msg", "删除失败，系统错误！");
            }

        } catch (Exception e) {
            logger.error("删除收藏异常！", e);
        }
    }

    public void _doReadingRecord(HttpServletRequest request, HttpServletResponse response) {

        try {

            List<ReadRecordDTO> readRecordDTOList = readLogDao.queryByUser_id(1);

            if (readRecordDTOList.size() > 0)
                request.setAttribute("readList", readRecordDTOList);
            else
                request.setAttribute("msg", "暂无数据。");

            new GenericServletBuilder(request, response, this.getClass()) //
                    .setUrl(Page.READING_RECORD.get()) //
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("会员阅读记录查询异常！", e);
        }
    }

    //会员记录删除
    public void _doDeleteRecorder(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
        } catch (Exception e) {
            logger.error("会员阅读记录查询异常！", e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {

        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        try {

            new GenericServletBuilder(request, response, this.getClass()) //
                    .build()
                    .getRequestMapping();

        } catch (Exception e) {
            logger.error("控制转发异常！", e);
        }
    }
}
